package lock;
/*
使用可重入锁实现线程的定制化通信
 */

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//第一步创建资源类，定义属性和操作方法
class shareResource{

//  定义标志位
  private int flag = 1;

  private Lock lock = new ReentrantLock();

//  创建3个condition
  private Condition c1 = lock.newCondition();
  private Condition c2 = lock.newCondition();
  private Condition c3 = lock.newCondition();

  public void print5(int loop) throws InterruptedException {

    lock.lock();

    try{

      while (flag != 1){
        c1.await();
      }
//      干活
      for (int i = 1 ; i <= 5;i++){
        System.out.println(Thread.currentThread().getName() + "::" + i + "：轮数：" + loop);
      }
//      通知
      flag = 2;
      c2.signal();

    }finally {
      lock.unlock();
    }
  }

  public void print10(int loop) throws InterruptedException {

    lock.lock();

    try{

      while (flag != 2){
        c2.await();
      }
//      干活
      for (int i = 1 ; i <= 10;i++){
        System.out.println(Thread.currentThread().getName() + "::" + i + "：轮数：" + loop);
      }
//      通知
      flag = 3;
      c3.signal();

    }finally {
      lock.unlock();
    }
  }

  public void print15(int loop) throws InterruptedException {

    lock.lock();

    try{

      while (flag != 3){
        c3.await();
      }
//      干活
      for (int i = 1 ; i <= 15;i++){
        System.out.println(Thread.currentThread().getName() + "::" + i + "：轮数：" + loop);
      }
//      通知
      flag = 1;
      c1.signal();

    }finally {
      lock.unlock();
    }
  }


}

//第三步创建多线程
public class ThreadDemo3 {

  public static void main(String[] args) {

    shareResource shareResource = new shareResource();

    new Thread(()->{
      for (int i = 1 ; i <= 10;i++){
        try {
          shareResource.print5(i);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    },"AA").start();

    new Thread(()->{
      for (int i = 1 ; i <= 10;i++){
        try {
          shareResource.print10(i);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    },"BB").start();

    new Thread(()->{
      for (int i = 1 ; i <= 10;i++){
        try {
          shareResource.print15(i);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    },"CC").start();
  }
}
